"""
Resaltado de sintaxis de C

Gabriel Acosta - 13/01/2014

"""


from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtGui


class Sintaxis(QSyntaxHighlighter):
    def __init__(self, parent, theme):
        QSyntaxHighlighter.__init__(self, parent)
        self.parent = parent
        palabraClave = QTextCharFormat()
        #funcionClave = QTextCharFormat()
        delimitador = QTextCharFormat()
        comentario = QTextCharFormat()
        comentario_2 = QTextCharFormat()
        include = QTextCharFormat()
        numeros = QTextCharFormat()
        texto_en_comillas = QTextCharFormat()

        self.highlightingRules = []

        # Palabras Reservadas
        color = QtGui.QColor(80, 100, 225)
        brush = QBrush(color, Qt.SolidPattern)
        palabraClave.setForeground(brush)
        palabraClave.setFontWeight(QFont.Bold)
        palabrasClaves = QStringList(["auto", "break", "case", "char", "const",
            "continue", "default", "do", "double", "else", "enum", "extern", "float",
            "for", "goto", "if", "int", "long", "register", "return", "short", "short",
            "signed", "sizeof", "static", "struct", "switch", "typedef",
            "union", "unsigned", "void", "volatile", "while"])

        for palabra in palabrasClaves:
            patron = QRegExp("\\b" + palabra + "\\b")
            regla = HighlightingRule(patron, palabraClave)
            self.highlightingRules.append(regla)

        # Delimitadores
        patron = QRegExp("[\)\(]+|[\{\}]+|[][]+")
        Color = QtGui.QColor(255, 255, 255)
        color = QBrush(Color, Qt.SolidPattern)
        delimitador.setForeground(color)
        delimitador.setFontWeight(QFont.Bold)
        regla = HighlightingRule(patron, delimitador)
        self.highlightingRules.append(regla)

        # Comentarios //
        color = QtGui.QColor(210, 170, 0)
        brush = QBrush(color, Qt.SolidPattern)
        patron = QRegExp("//[^\n]*")
        comentario.setForeground(brush)
        regla = HighlightingRule(patron, comentario)
        self.highlightingRules.append(regla)

        # Comentarios /* */
        #FIXME: No funciona por ahora, solo comenta en una linea
        color = QtGui.QColor(210, 170, 0)
        brush = QBrush(color, Qt.SolidPattern)
        patron = QRegExp("\/*.*\*/")
        comentario_2.setForeground(brush)
        regla = HighlightingRule(patron, comentario_2)
        self.highlightingRules.append(regla)

        # Includes
        color = QtGui.QColor(255, 102, 50)
        brush = QBrush(color, Qt.SolidPattern)
        patron = QRegExp("#[^\n]*")
        include.setForeground(brush)
        regla = HighlightingRule(patron, include)
        self.highlightingRules.append(regla)

        # Numeros
        patron = QRegExp("[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?")
        color = QtGui.QColor(130, 180, 0)
        patron.setMinimal(True)
        numeros.setForeground(color)
        regla = HighlightingRule(patron, numeros)
        self.highlightingRules.append(regla)

        # Texto en comillas
        color = QtGui.QColor(240, 180, 0)
        brush = QBrush(color, Qt.SolidPattern)
        patron = QRegExp("\".*\"")
        texto_en_comillas.setForeground(brush)
        regla = HighlightingRule(patron, texto_en_comillas)
        self.highlightingRules.append(regla)

    def highlightBlock(self, text):
        for regla in self.highlightingRules:
            expresion = QRegExp(regla.patron)
            index = expresion.indexIn(text)
            while index >= 0:
                length = expresion.matchedLength()
                self.setFormat(index, length, regla.format)
                index = text.indexOf(expresion, index + length)
        self.setCurrentBlockState(0)


class HighlightingRule():

    def __init__(self, patron, format):
        self.patron = patron
        self.format = format
